package com.yxqz.manage_course.service;

import com.xuecheng.framework.domain.course.CourseBase;
import com.xuecheng.framework.domain.course.Teachplan;
import com.xuecheng.framework.domain.course.ext.TeachplanNode;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.yxqz.manage_course.dao.CourseBaseRepository;
import com.yxqz.manage_course.dao.TeachPlanMapper;
import com.yxqz.manage_course.dao.TeachPlanRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

/**
 * @author 鱼香茄子
 * @data 2021/10/29
 * 课程管理服务
 */

@Service
public class CourseService {

    //    注入教学计划Mapper
    @Autowired
    TeachPlanMapper teachPlanMapper;


    //    注入教学计划使用jpa
    @Autowired
    TeachPlanRepository teachPlanRepository;


    @Autowired
    CourseBaseRepository courseBaseRepository;

    /**
     * 程计划查询
     * 课程计划查询 通过课程id
     *
     * @param courseId 课程id
     * @return TeachplanNode 课程节点信息
     */
    public TeachplanNode findTeachPlanList(String courseId) {
        return teachPlanMapper.findTeachPlanList(courseId);
    }

    /**
     * 添加课程计划
     * 处理 parentId
     *
     * @param teachplan 课程计划
     * @return ResponseResult
     * Transactional 需要添加事物控制
     * mysql有事物，必须要添加这个注解哦
     */
    @Transactional
    public ResponseResult addTeachPlan(Teachplan teachplan) {
//        判断Teachplan 结点
        if (
                teachplan == null ||
                        StringUtils.isEmpty(teachplan.getCourseid()) ||
                        StringUtils.isEmpty((teachplan.getPname()))
        ) {
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        }

//        得到课程的id
        String courseid = teachplan.getCourseid();
        String parentid = teachplan.getParentid();
//            取出课程的根节点
        if (parentid == null) {
            parentid = this.getTeachPlanRoot(courseid);
        }


//        新节点
        Teachplan teachplanNew = new Teachplan();
//        将页面的传过来的的对象的信息，拷贝到新的Teachplan 使用BeanUitls工具类
        BeanUtils.copyProperties(teachplan, teachplanNew);
//       给新的Teachplan添加课程id和节点id
        teachplanNew.setCourseid(courseid);
        teachplanNew.setParentid(parentid);



//        获取父节点的Teachplan基本信息通过jpa
        Optional<Teachplan> optional = teachPlanRepository.findById(parentid);
        if (!optional.isPresent()) {
            return null;
        }
        Teachplan teachplanFather = optional.get();
        String fatherGrade = teachplanFather.getGrade();// 获取父节点的级别

        if (fatherGrade.equals("1")) {
            teachplanNew.setGrade("2");//节点等级 通过父节点的级别来设置
        } else {
            teachplanNew.setGrade("3");//节点等级 通过父节点的级别来设置
        }

        teachPlanRepository.save(teachplanNew);
        return new ResponseResult(CommonCode.SUCCESS);

    }








    /**
     * 查询课程的根节点，如果查询不到则自动添加根节点
     *
     * @param courseId 课程id
     * @return 节点id
     */
    private String getTeachPlanRoot(String courseId) {

        Optional<CourseBase> optional = courseBaseRepository.findById(courseId);

        if (!optional.isPresent()) {
            return null;
        }

//        获取课程信息
        CourseBase courseBase = optional.get();

        List<Teachplan> teachplanList = teachPlanRepository.findTeachplanByCourseidAndParentid(courseId, "0");

        //            查询不到，自动添加一个根节点
        if (teachplanList == null || teachplanList.size() <= 0) {
            Teachplan teachplan = new Teachplan();
            teachplan.setParentid("0");
            teachplan.setGrade("1");
            teachplan.setPname(courseBase.getName());
            teachplan.setStatus("0");
            Teachplan save = teachPlanRepository.save(teachplan);
            return save.getId();
        }
        return teachplanList.get(0).getId();
    }







}

